package basic.lessons;

public class CustomCalc {


	public static void main(String[] args) {
		String expression = "1";
		while (!expression.equalsIgnoreCase("0")) {

			System.out.println("Please, enter expression and  press \"Enter\"");
			expression = TextIO.getlnString().toString();
			if (expression.equalsIgnoreCase("0")) {
				System.out.println("Program exits as 0 was entered");
				break;
			}
			int operatorPosition = 0; // holds operator position
			char operator = 0;            // holds operator type
			
			int plusOpPositon = expression.indexOf("+"); // finding index of +  operator within  expression
				if (plusOpPositon>0) {
					operatorPosition = plusOpPositon;
				    operator = expression.charAt(operatorPosition);
				}
				
			int minusOpPositon = expression.indexOf("-"); // finding index of - operator within  expression
			    if (minusOpPositon>0){
				    operatorPosition = minusOpPositon;			
			        operator = expression.charAt(operatorPosition);
			    }
			        
			int multiplyOpPositon = expression.indexOf("*"); // finding index of * operator within  expression
			    if (multiplyOpPositon>0){
			    	operatorPosition = multiplyOpPositon;			
			        operator = expression.charAt(operatorPosition);
			    }
			int divideOpPositon = expression.indexOf("/"); // finding index of / operator within  expression
				if (divideOpPositon>0){
					operatorPosition = divideOpPositon;
					operator = expression.charAt(operatorPosition);
				}				

			String operandLeft = null;  // Initializing operands
			String operandRight = null;
			
			operandLeft = expression.substring(0, operatorPosition); // extracting operands from expression
			operandRight = expression.substring(operatorPosition+1, expression.length());
			
			System.out.println("operandLeft value = " + operandLeft);
			System.out.println("operandRight value = " + operandRight);
			
			Double doubleOperandLeft = Double.valueOf(operandLeft); // converting operands from String to Double
			Double doubleoperandRight = Double.valueOf(operandRight);
		
			double expressionValue = 0; // holds value of expression
			switch (operator) {
			case '+':
				expressionValue = doubleOperandLeft+doubleoperandRight;
				break;
			case '-':
				expressionValue = doubleOperandLeft-doubleoperandRight;
				break;
			case '*':
				expressionValue = doubleOperandLeft*doubleoperandRight;
				break;
			case '/':
				expressionValue = doubleOperandLeft/doubleoperandRight;
				break;
				
			}
			System.out.println("Expression value = " + expressionValue); // prints the result
		
			
		}


	}

}
